package com.cmcc.groupcontacts.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yinhai.activity.R;


public class MyTitlePanel extends LinearLayout {

	public MyTitlePanel(Context context) {
		this(context, null);
		this.context = context;
	}

	/**
	 * @author zhyy
	 */
	public interface OnOptionClickListener {
		void onOptionClick(View v);
	}

	/**
	 * @author zhyy
	 */
	public interface OnBackClickListener {
		void onBackClick(View v);
	}

	/**
	 * @author zhyy
	 */
	public interface OnCommandClickListener {
		void onCommandClick(View v);
	}

	// -------------------------------------------------

	Activity activity;

	Context context;

	/**
	 * 标题
	 */
	TextView titleTextView;
	/**
	 * 返回按钮
	 */
	View backButton;
	/**
	 * 命令按钮
	 */
	View CommandButton;

	// ------------------------------------------------------------------
	OnOptionClickListener onOptionClickListener;
	OnBackClickListener onBackClickListener;
	OnCommandClickListener onCommandClickListener;

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public void setOnOptionClickListener(
			OnOptionClickListener onOptionClickListener) {
		this.onOptionClickListener = onOptionClickListener;
	}

	public void setOnBackClickListener(OnBackClickListener onBackClickListener) {
		this.onBackClickListener = onBackClickListener;
	}

	public void setOnCommandClickListener(
			OnCommandClickListener onCommandClickListener) {
		this.onCommandClickListener = onCommandClickListener;
	}

	// ------------------------------------------------------------------

	public MyTitlePanel(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		// SharedPreferences sp =
		// context.getSharedPreferences(Constants.SYSTEM_CONFIG_INI, 0);
		// int themeSelect = sp.getInt(Constants.THEME_CONFIG_SELECT, 0);
		// if (themeSelect == 0)
		// LayoutInflater.from(context).inflate(R.layout.sty0_title_panel, this,
		// true);
		// else
		// LayoutInflater.from(context).inflate(R.layout.sty1_title_panel, this,
		// true);

		LayoutInflater.from(context).inflate(R.layout.sty0_title_panel, this,
				true);

		initView();

		initAttire(context, attrs);

	}

	private void initAttire(Context context, AttributeSet attr) {
		TypedArray a = context.obtainStyledAttributes(attr,
				R.styleable.TitlePanel);
		int showOption = a.getInt(R.styleable.TitlePanel_showOption, 0);
		int showBack = a.getInt(R.styleable.TitlePanel_showBack, 0);
		int showCommand = a.getInt(R.styleable.TitlePanel_showCommand, 0);
		String titleText = a.getString(R.styleable.TitlePanel_titleText);
		String commandText = a.getString(R.styleable.TitlePanel_commandText);

		a.recycle();

		backButton.setVisibility(showBack);
		CommandButton.setVisibility(showCommand);
		// CommandButton.setText(commandText);

		titleTextView.setText(titleText);

		if (showOption == 0) {
			titleTextView
					.setCompoundDrawablesWithIntrinsicBounds(
							null,
							null,
							getResources().getDrawable(
									R.drawable.ico_title_more), null);
		} else {
			titleTextView.setCompoundDrawablesWithIntrinsicBounds(null, null,
					null, null);
		}
	}

	private void initView() {

		titleTextView = (TextView) this.findViewById(R.id.title_panel_text);
		titleTextView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onOptionClickListener != null) {
					onOptionClickListener.onOptionClick(v);
				}
			}
		});

		backButton = (Button) this.findViewById(R.id.title_panel_back_button);
		backButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				if (onBackClickListener != null)
					onBackClickListener.onBackClick(v);
				else if (activity != null) {
					activity.onBackPressed();
				}

			}
		});

		CommandButton = this.findViewById(R.id.title_panel_command);
		CommandButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				if (onCommandClickListener != null)
					onCommandClickListener.onCommandClick(v);

			}
		});

	}

	public void setTitle(String title) {
		titleTextView.setText(title);
	}

	public String getTitle() {
		return titleTextView.getText().toString();
	}

	public View getBackButton() {
		return backButton;
	}

	public View getCommandButton() {
		return CommandButton;
	}
}
